package cn.ly.core.proxy.jdkproxy;

import java.lang.reflect.InvocationHandler;  
import java.lang.reflect.Method;  
import java.lang.reflect.Proxy;  
  
public class BookFacadeProxy implements InvocationHandler {  
    private Object target;  
  
    /** 
     *  
     * @param target 
     * @return 
     */  
    public Object bind(Object target) {  
        this.target = target;  
        // 取得代理对象  
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),  
                target.getClass().getInterfaces(), this);  
    }  
  
    @Override
    /**
     * jdk动态代理也使用反射创建proxy
     */
    public Object invoke(Object proxy, Method method, Object[] args)  
            throws Throwable {  
        Object result=null;  
        Method[] m = proxy.getClass().getMethods();
        for(Method m1 : m){
        	System.out.println(m1.getName());
        }
        System.out.println("Proxy start...");  
        System.out.println("method name:"+method.getName());  
        result=method.invoke(target, args);  
        System.out.println("Proxy end...");  
        return result;  
    }  
    
    
    public static void main(String[] args) {
        BookFacadeProxy proxy = new BookFacadeProxy();  
        BookFacade bookProxy = (BookFacade) proxy.bind(new BookFacadeImpl());  
        bookProxy.addBook();  
        bookProxy.deleteBook();
    }  
  
}  

class BookFacadeImpl implements BookFacade {  

	BookFacade target = null;
	  
    @Override  
    public void addBook() {  
        System.out.println("add book logic is running。。。");   
    }  
  
    @Override  
    public void deleteBook() {  
        System.out.println("delete book logic is running。。。");  
          
    }  
    
    
    /*public BookFacade getProxy(BookFacade target1) {
    	this.target = target1;
    	return (BookFacadeImpl) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces()
    			, new InvocationHandler(){
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						Object result=null;  
				        System.out.println("Proxy start...");  
				        System.out.println("method name:"+method.getName());  
				        result=method.invoke(target, args);  
				        System.out.println("Proxy end...");  
				        return result; 
					}
    	});
    	
	}*/
      
} 